package com.wzh.house.controller;

import java.util.Arrays;
import java.util.Map;

import com.wzh.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.house.entity.RentUserAttentionEntity;
import com.wzh.house.service.RentUserAttentionService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-03-22 19:04:17
 */
@RestController
@RequestMapping("house/rentuserattention")
public class RentUserAttentionController {
    @Autowired
    private RentUserAttentionService rentUserAttentionService;

    @Autowired
    private JwtUtil jwtUtil;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = rentUserAttentionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{rentId}")
    public R info(@PathVariable("rentId") Integer rentId){
		RentUserAttentionEntity rentUserAttention = rentUserAttentionService.getById(rentId);

        return R.ok().put("rentUserAttention", rentUserAttention);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody RentUserAttentionEntity rentUserAttention, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null) {
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null&&"user".equals(claims.get("roles"))) {
                rentUserAttention.setUserId(Integer.valueOf(claims.getId()));
                if (rentUserAttention.getState()){
                    //添加
                    rentUserAttentionService.add(rentUserAttention);
                    return R.ok().put("state",false);
                }else {
                    //删除
                    rentUserAttentionService.del(rentUserAttention);
                    return R.ok().put("state",true);
                }
            }
        }
        return R.error("用户未登录");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RentUserAttentionEntity rentUserAttention){
		rentUserAttentionService.updateById(rentUserAttention);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] rentIds){
		rentUserAttentionService.removeByIds(Arrays.asList(rentIds));

        return R.ok();
    }

}
